package com.koron.system.agent.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.bean.system.agent.po.AgentRoleUserPo;
import com.koron.system.agent.mapper.AgentRoleUserMapper;
import com.koron.system.agent.service.AgentRoleUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 代理用户关联角色表(AgentRoleUser)表服务实现类
 *
 * @author zhouj
 * @since 2021-11-23 18:03:34
 */
@Service("agentRoleUserService")
public class AgentRoleUserServiceImpl implements AgentRoleUserService {

    @Autowired
    AgentRoleUserMapper agentRoleUserMapper;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(AgentRoleUserPo agentRoleUser) {

        return agentRoleUserMapper.insertOrUpdate(agentRoleUser);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(List<AgentRoleUserPo> agentRoleUserList) {

        if (CollectionUtils.isEmpty(agentRoleUserList)) {
            return 0;
        }
        //对列表去重
        Map<String, String> cond = new HashMap<>();
        agentRoleUserList.forEach(p -> {
            cond.put(p.getLeader(), p.getAgent());
        });
        Set<String> keys = cond.keySet();
        for (String key : keys) {
            //删除 leader-agent 角色记录
            deleteByAgent(key, cond.get(key));
        }

        return agentRoleUserMapper.insertOrUpdateBatch(agentRoleUserList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(AgentRoleUserPo agentRoleUser) {

        return agentRoleUserMapper.insert(agentRoleUser);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(List<AgentRoleUserPo> agentRoleUserList) {
        //入库前 先删除改账号下代理角色

        return agentRoleUserMapper.insertBatch(agentRoleUserList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(AgentRoleUserPo agentRoleUser) {
        return agentRoleUserMapper.update(agentRoleUser);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(String id) {

        return agentRoleUserMapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public AgentRoleUserPo queryById(String id) {

        return agentRoleUserMapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<AgentRoleUserPo> queryList(AgentRoleUserPo agentRoleUser) {
        return agentRoleUserMapper.queryList(agentRoleUser);
    }

    @Override
    public int deleteByAgent(String leader, String agent) {
        return agentRoleUserMapper.deleteByAgent(leader, agent);
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(AgentRoleUserPo agentRoleUser, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());

        List<AgentRoleUserPo> list = agentRoleUserMapper.queryList(agentRoleUser);

        return new PageInfo(list);
    }

}
